<script setup lang="ts">
import { onMounted, ref } from 'vue';

import { Page } from '@vben/common-ui';
import { IconifyIcon } from '@vben/icons';

import { getBusinessContexts, getServiceItems } from '#/api/booking';

import BookingForm from './components/BookingForm.vue';
import MyBookings from './components/MyBookings.vue';
import ServiceList from './components/ServiceList.vue';

interface BusinessContext {
  id: string;
  name: string;
  description: string;
  icon: string;
  status: number;
}

interface ServiceItem {
  id: string;
  name: string;
  description: string;
  price: number;
  duration: number;
  contextId: string;
  status: number;
}

const businessContexts = ref<BusinessContext[]>([]);
const serviceItems = ref<ServiceItem[]>([]);
const selectedContext = ref<BusinessContext | null>(null);
const selectedService = ref<null | ServiceItem>(null);

const selectContext = async (context: BusinessContext) => {
  selectedContext.value = context;
  selectedService.value = null;

  // 加载该业务场景的服务项目
  try {
    const services = await getServiceItems({ contextId: context.id });
    console.log('加载的服务项目数据:', services);
    serviceItems.value = services || [];
  } catch (error) {
    console.error('加载服务项目失败:', error);
    serviceItems.value = [];
  }
};

const handleServiceSelected = (service: ServiceItem) => {
  selectedService.value = service;
};

const handleBookingSubmitted = (booking: any) => {
  // 预约提交成功后的处理
  console.log('预约提交成功:', booking);
  // 可以跳转到排队状态页面或显示成功提示
};

const loadBusinessContexts = async () => {
  try {
    const contexts = await getBusinessContexts();
    console.log('加载的业务场景数据:', contexts);
    businessContexts.value = contexts || [];
  } catch (error) {
    console.error('加载业务场景失败:', error);
    businessContexts.value = [];
  }
};

onMounted(() => {
  loadBusinessContexts();
});
</script>

<template>
  <div class="booking-system">
    <Page title="预约排队系统" content="多业态预约排队取号管理">
      <div class="booking-container">
        <!-- 业务场景选择 -->
        <div class="business-contexts">
          <h2 class="mb-4 text-lg font-semibold">选择业务场景</h2>
          <div class="context-grid">
            <div
              v-for="context in businessContexts"
              :key="context.id"
              class="context-card"
              :class="{ active: selectedContext?.id === context.id }"
              @click="selectContext(context)"
            >
              <div class="context-icon">
                <IconifyIcon :icon="context.icon" />
              </div>
              <div class="context-info">
                <h3 class="context-name">{{ context.name }}</h3>
                <p class="context-desc">{{ context.description }}</p>
              </div>
            </div>
          </div>
        </div>

        <!-- 服务项目展示 -->
        <div v-if="selectedContext" class="service-section">
          <h2 class="mb-4 text-lg font-semibold">
            {{ selectedContext.name }} - 服务项目
          </h2>
          <ServiceList
            :services="serviceItems"
            :context-id="selectedContext.id"
            @service-selected="handleServiceSelected"
          />
        </div>

        <!-- 预约表单 -->
        <div v-if="selectedService" class="booking-form-section">
          <h2 class="mb-4 text-lg font-semibold">预约信息</h2>
          <BookingForm
            :service="selectedService"
            :context="selectedContext"
            @booking-submitted="handleBookingSubmitted"
          />
        </div>

        <!-- 我的预约 -->
        <div class="my-bookings-section">
          <h2 class="mb-4 text-lg font-semibold">我的预约</h2>
          <MyBookings />
        </div>
      </div>
    </Page>
  </div>
</template>

<style scoped>
.booking-system {
  padding: 20px;
}

.booking-container {
  max-width: 1200px;
  margin: 0 auto;
}

.business-contexts {
  margin-bottom: 40px;
}

.context-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 20px;
}

.context-card {
  display: flex;
  align-items: center;
  padding: 20px;
  cursor: pointer;
  background: white;
  border: 2px solid #e5e7eb;
  border-radius: 12px;
  transition: all 0.3s ease;
}

.context-card:hover {
  border-color: #3b82f6;
  box-shadow: 0 4px 12px rgb(59 130 246 / 10%);
}

.context-card.active {
  background: #f0f9ff;
  border-color: #3b82f6;
}

.context-icon {
  margin-right: 16px;
  color: #3b82f6;
}

.context-info {
  flex: 1;
}

.context-name {
  margin-bottom: 4px;
  font-size: 16px;
  font-weight: 600;
  color: #1f2937;
}

.context-desc {
  margin: 0;
  font-size: 14px;
  color: #6b7280;
}

.service-section,
.booking-form-section,
.my-bookings-section {
  margin-bottom: 40px;
}
</style>
